import 'package:hrf_app/common/index.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'index.dart';

class LoginPage extends GetView<LoginController> {
  const LoginPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LoginController>(
      init: LoginController(),
      id: "login",
      builder: (_) {
        return Scaffold(
          body: SafeArea(
            child: ListView(children: <Widget>[
              _buildIp(),
              SizedBox(height: 80),
              _buildLogo(),
              SizedBox(height: 10),
              Container(
                  margin: EdgeInsets.all(20),
                  // decoration: BoxDecoration(
                  //     color: Colors.white
                  // ),
                  child: SizedBox(
                    height: 240,
                    child: _buildForm(_),
                  ))
            ]),
          ),
        );
      },
    );
  }

  Widget _buildLogo() {
    return Container(
        child: SizedBox(
          height: 160.0,
          child: TDImage(
            assetUrl: AssetsImages.hrfLogoPng,
            fit: BoxFit.fitHeight,
            type: TDImageType.circle,
          ),
        ));
  }

  /// 设置ip 跳转页面
  _buildIp() {
    return Container(
        alignment: Alignment.center,
        child: Row(mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[
          TDButton(
            size: TDButtonSize.small,
            type: TDButtonType.text,
            onTap: () => controller.onSetIp(),
            text: '设置',
          )
        ]));
  }

  Widget _buildForm(LoginController controller) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        _buildUsernameField(controller),
        const SizedBox(height: 0),
        _buildPasswordField(controller),
        const SizedBox(height: 0),
        Row(children: [
          Expanded(flex: 1, child: _buildLoginButton(controller)),
        ])
      ],
    );
  }

  Widget _buildUsernameField(LoginController controller) {
    return Column(children: [ TDInput(
      type: TDInputType.normal,
      controller: controller.usernameController,
      leftLabel: '账号',
      hintText: '请输入密码',
      backgroundColor: Colors.white,

      onBtnTap: () {

      },
      needClear: false,
    ),
      const SizedBox(
        height: 16,
      ),
    ]);
  }

  Widget _buildPasswordField(LoginController controller) {
    return Column(children: [ TDInput(
      type: TDInputType.normal,
      controller: controller.passwordController,
      obscureText: !controller.isPasswordVisible,
      leftLabel: '密码',
      hintText: '请输入密码',
      backgroundColor: Colors.white,
      rightBtn: controller.isPasswordVisible
          ? Icon(
        TDIcons.browse,
        color: TDTheme
            .of(Get.context!)
            .fontGyColor3,
      )
          : Icon(
        TDIcons.browse_off,
        color: TDTheme
            .of(Get.context!)
            .fontGyColor3,
      ),
      onBtnTap: () {
        controller.togglePasswordVisibility();
      },
      needClear: false,
    ),
      const SizedBox(
        height: 16,
      ),
    ]);
  }

  Widget _buildLoginButton(LoginController controller) {
    return TDButton(
      text: '登录',
      margin: EdgeInsets.symmetric(horizontal: 20),
      size: TDButtonSize.large,
      type: TDButtonType.fill,
      shape: TDButtonShape.rectangle,
      theme: TDButtonTheme.primary,
      onTap: () {
        controller.onSignIn();
      },
    );
  }
}
